/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utils;

import java.util.Vector;
import javax.microedition.lcdui.Font;

/**
 *
 * @author cristian
 */
public class StringUtils {

    public static String[] split(String str, String separator) {
        Vector v = new Vector();
        int pIndex = 0, nIndex;

        while((nIndex = str.indexOf(separator, pIndex)) != -1) {
            v.addElement(str.substring(pIndex, nIndex));
            pIndex = nIndex+1;
        }
        v.addElement(str.substring(pIndex+1));

        String[] arr = new String[v.size()];
        v.copyInto(arr);
        return arr;
    }

    public static String join(String[] arr, String separator) {
        if(arr.length == 0)
            return "";

        StringBuffer buffer = new StringBuffer(arr[0]);
        for(int i=1; i<arr.length; i++) {
            buffer.append(separator);
            buffer.append(arr[i]);
        }            
        return buffer.toString();
    }

    public static String[] adjustToWidth(String str, Font font, int maxWidth, char separator) {
        if(font.stringWidth(str) < maxWidth)
            return new String[]{str};

        Vector v = new Vector();
        int sep = (int)separator;
        int pIndex = 0, nIndex = str.indexOf(sep, pIndex);

        while(nIndex < str.length()) {
            while(font.substringWidth(str, pIndex, nIndex-pIndex) < maxWidth && nIndex < str.length())
                nIndex = str.indexOf(sep, nIndex+1) == -1 ? str.length() : str.indexOf(sep, nIndex+1);
            
            if(nIndex == str.length()) {
                if(font.substringWidth(str, pIndex, nIndex-pIndex) > maxWidth) {
                    while(font.substringWidth(str, pIndex, nIndex-pIndex) > maxWidth) {
                        nIndex = str.lastIndexOf(sep, nIndex-1);
                    }
                    v.addElement(str.substring(pIndex, nIndex));
                    v.addElement(str.substring(nIndex+1));
                    nIndex = str.length(); // in order to exit the while loop
                }
                else
                    v.addElement(str.substring(pIndex, nIndex));
            } else {
                v.addElement(str.substring(pIndex, str.lastIndexOf(sep, nIndex-1)));
                pIndex = str.lastIndexOf(sep, nIndex-1)+1;
                nIndex = str.indexOf(sep, nIndex+1) == -1 ? str.length() : str.indexOf(sep, nIndex+1);
            }            
        }
        String[] result = new String[v.size()];
        v.copyInto(result);
        return result;
    }

    public static String replace(String str, String strToFind, String strToReplace) {
        StringBuffer buffer = new StringBuffer();
        int pIndex = 0, nIndex;
        while((nIndex = str.indexOf(strToFind)) != -1) {
            buffer.append(str.substring(pIndex, nIndex));
            buffer.append(strToReplace);
            buffer.append(str.substring(nIndex+1));
            pIndex = nIndex+1;
        }
        buffer.append(str.substring(pIndex));
        return buffer.toString();
    }

}
